<route lang="json5" type="page">
{
	style: {
		navigationStyle: 'custom',
		navigationBarTitleText: '图书拓展资源',
	},
}
</route>

<template>
	<view class="page-book-details">
		<fly-navbar leftText="图书拓展资源" fixed />
		<scroll-view
			scroll-y
			:class="['w-full']"
			:style="{ height: contentHeight + 'rpx' }"
			v-if="thisPage === 1 && detailsInfo.length"
		>
			<view class="w-full py-6 px-4 box-border">
				<view class="w-full">
					<view
						class="w-full bg-white rounded-[25rpx] flex justify-between items-center px-4 py-4 box-border mb-2 last:mb-0 read-item"
						v-for="item in detailsInfo"
						:key="item.id"
						@tap="toResource(item.id, item.ext)"
					>
						<text class="text-[#303030] text-[26rpx]">{{ item.name }}</text>
						<image
							class="w-[36rpx] h-[36rpx]"
							v-if="item.ext === 1"
							:src="baseImg + '/book/book-video.png'"
						></image>
						<image
							class="w-[36rpx] h-[36rpx]"
							v-if="item.ext === 2"
							:src="baseImg + '/book/book-music.png'"
						></image>
						<image
							class="w-[36rpx] h-[36rpx]"
							v-if="item.ext === 4"
							:src="baseImg + '/book/book-doc.png'"
						></image>
						<image
							class="w-[36rpx] h-[36rpx]"
							v-if="item.ext === 8"
							:src="baseImg + '/book/book-doc.png'"
						></image>
					</view>
				</view>
			</view>
		</scroll-view>
		<no-page
			v-if="thisPage === 0 || (thisPage === 1 && !detailsInfo.length)"
			:type="type"
			:title="title"
			@click="retry"
		></no-page>
	</view>
</template>

<script lang="ts" setup>
import { storeToRefs } from 'pinia';
import { systemInfo } from '@/utils/getSystemInfo';
import { getBookResourceClass } from '@/service/book';
import { useUserStore } from '@/store';
import { toAuthPage } from '@/utils/jump';
import onShare from '@/hooks/useShare';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { onShareAppMessage, onShareTimeline } = onShare();
const baseImg = ref(import.meta.env.VITE_STATIC_IMAGE);
const { contentHeight } = systemInfo();
const detailsInfo = ref(null);
const title = ref('');
const type = ref('btn');
const btnTitle = ref('重试');
const thisPage = ref(-1);
const query = ref(null);
const userStore = useUserStore();
const { userInfo } = storeToRefs(userStore);

onLoad(options => {
	if (!userInfo.value.student_id) {
		// 分享或者进入未登录
		thisPage.value = 0;
		type.value = 'btn';
		title.value = '还没有登录';
		btnTitle.value = '登录';
		uni.hideShareMenu({ hideShareItems: ['qq', 'qzone'] });
		return;
	}
	if (options) {
		query.value = options;
	} else {
		thisPage.value = 0;
		type.value = 'msg';
		title.value = '并未查询到该书籍,请返回重试';
	}
});

onShow(() => {
	if (query.value.id) {
		getDetailsInfo(query.value.id);
	}
});

const getDetailsInfo = async (id: number) => {
	uni.showLoading({ title: '加载中...' });
	try {
		const res = await getBookResourceClass({
			book_id: Number(id),
		});
		uni.hideLoading();
		if (res.status_code === 200 && res.data) {
			thisPage.value = 1;
			detailsInfo.value = res.data;
			if (!res.data.length) {
				type.value = 'msg';
				title.value = '暂无图书拓展资源';
			}
		} else {
			thisPage.value = 0;
			title.value = res.message;
		}
	} catch (error) {
		uni.hideLoading();
		thisPage.value = 0;
		title.value = '服务器异常';
	}
};

const retry = () => {
	if (!userInfo.value.student_id) {
		// 分享或者进入未登录
		toAuthPage({}, { type: 'switchTab', url: '/pages/index/index' });
		return;
	}
	getDetailsInfo(query.value.id);
};

const toResource = (id: number, ext: number) => {
	uni.navigateTo({
		url: `/pages-book/resource/details/index?resource_id=${id}&book_id=${query.value.id}&ext=${ext}`,
	});
};
</script>

<style lang="scss">
page {
	background-color: #fff;
}
</style>
<style lang="scss" scoped>
.read-item {
	box-shadow:
		0rpx 0rpx 3rpx 0rpx rgb(0 0 0 / 3%),
		0rpx 5rpx 21rpx 0rpx rgb(0 0 0 / 7%);
}
</style>
